package com.cetc.expt;

import com.cetc.expt.utils.ProxyUtils;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author louzhuangxi
 * @Classname CglibDynamicProxy
 * @Description cglib动态代理 实现代码日志功能增强
 * @Date 2022/12/15 20:04
 */
public class CglibDynamicProxy {
    public static void main(String[] args) {
        Programmer programmer = new Programmer();

        Hacker hacker = new Hacker();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(programmer.getClass());
        enhancer.setCallback(hacker);
        Programmer proxy = (Programmer) enhancer.create();
        System.out.println(proxy.getClass());
        String res = proxy.code();
        System.out.println(res);
    }
}

class Programmer {
    public String code() {
        System.out.println("I'm a Programmer,Just Coding.......");
        return "success";
    }
}

class Hacker implements MethodInterceptor {

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("**** I am a hacker,Let's see what the poor programmer is doing Now...");
        Object res = methodProxy.invokeSuper(o, objects);
        System.out.println("****  Oh,what a poor programmer.....");
        return res;
    }
}



